package com.hotelManagerSystem.controller;

import com.hotelManagerSystem.model.*;
import com.hotelManagerSystem.service.IDepartmentService;
import com.hotelManagerSystem.service.IRoleDepartmentService;
import com.hotelManagerSystem.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * <p>
 *  角色--部门 前端控制器
 * </p>
 *
 * @author longyajr
 * @since 2021-12-22
 */
@Controller
@RequestMapping("role-department")
public class RoleDepartmentController {
    @Autowired
    private IRoleService iRoleService;
    @Autowired
    private IDepartmentService iDepartmentService;
    @Autowired
    private IRoleDepartmentService iRoleDepartmentService;

    //修改角色部门页面跳转
    @RequestMapping("FPDepartmentUI")
    public String FPDepartmentUI(Model model, String roleIdStr){
//        System.out.println("------------FPDepartmentUI---------------");
        int roleId = Integer.parseInt(roleIdStr);
        Role role_db = iRoleService.getById(roleId);
        List<Department> department_dbList = iDepartmentService.list();
        model.addAttribute("role_db",role_db);
        model.addAttribute("department_dbList",department_dbList);

        return "";
    }

//    修改角色权限
    @RequestMapping("FPDepartment")
    public String FPDepartment(Model model, RoleDepartment roleDepartment, String roleIdStr, String[] permissionIdStrs){
//        System.out.println("------------FPDepartment---------------");
        int roleId = Integer.parseInt(roleIdStr);
//        查找数据库是否有这个角色的权限信息，有就刪
        boolean m = iRoleDepartmentService.deleteRoleDepartmentByRoleId(roleId);

        for(String permissionIdStr:permissionIdStrs){
            int permissionId = Integer.parseInt(permissionIdStr);
//            装配role-permission
            roleDepartment.setRoleId(roleId);
            roleDepartment.setDepartmentId(permissionId);
            boolean b = iRoleDepartmentService.saveOrUpdate(roleDepartment);
        }

//        跳转回角色列表页面
//        return list(model);

        List<Role> role_dbList = iRoleService.queryRoleList();
        model.addAttribute("role_dbList",role_dbList);

        return "";
    }
}
